<template>
	<div class="activitiesTwoPro" v-if="productdata.length>0">
		<div class="proul">
			<active-item v-if="isApp" :buttonActive="TwoproductStyle == 'E' ?  buttonActive.bind(this,val,1) : buttonActive.bind(this,val,2) " :itemType="TwoproductStyle" v-for="(val,index) in productdata" :key="index" :detail="val"></active-item>
			<active-item v-if="!isApp" :buttonActive="TwoproductStyle == 'E' ?  null : buttonActive.bind(this,val) " :itemType="TwoproductStyle" v-for="(val,index) in productdata" :key="index" :detail="val"></active-item>
			<div v-if="productdata.length==0 && env === 'edit'">暂无数据</div>
			<div style="clear: both;"></div>
		</div>
	</div>

</template>

<script>
	import { getPageNameOrVirtualPageName } from "../../utils/getPageName";
	import { queryGoodsList } from '@/utils/ReturnAxios';
	import ActiveItem from '../ActiveItem';
	import { ADD_CAR , NAVIGATE} from "../../config/postMessage-types";
	export default {
		props: {
			goodsInfo: {
				type: Array,
				default: () => []
			},
			twoproid: {
				type: String,
				default: ""
			},
		},
		data() {
			return {
				codeList: [],
				productdata: [],
				isOnLoad: true,
			}
		},
		components: {
			ActiveItem
		},
		methods: {
			goodsCodearrey() {
				this.codeList = []
				this.productdata = []
				if(this.goodsInfo.length == 0) {
					return
				}
				if(this.goodsInfo && this.goodsInfo.length > 0) {
					for(var i = 0; i < this.goodsInfo.length; i++) {
						this.productdata.push(this.goodsInfo[i].goodsNewVO)
						//this.codeList.push(this.goodsInfo[i].goodsCode)
					}
					this.getdetaildata()
					//this.queryDetail()
				}
			},
			async queryDetail() {
				this.isOnLoad = true
				var goodscodeList = this.codeList
				if(goodscodeList.length == 0) {
					return
				}
				var config = {
					goodscodeList: goodscodeList
				}
				var res = await queryGoodsList(config)
				res = res.data
				this.productdata = res.data[0]
				this.isOnLoad = false
				this.getdetaildata()
				if(!res.returnCode) {
					return
				}
			},
			getdetaildata() {
				for(var i = 0; i < this.goodsInfo.length; i++) {
					for(var j = 0; j < this.productdata.length; j++) {
						if(this.goodsInfo[i].goodsCode == this.productdata[j].goodsCode) {
							this.productdata[j].mainPic = this.goodsInfo[i].publicityPicture
							this.productdata[j].regularTypeLabel = this.goodsInfo[i].relTitle
						}
					}
				}
			},
			buttonActive(val, num) {
				if(num == 1 && this.isApp) {
					this.addcar(val)
				} else if(num == 2 && this.isApp) {
					let goBackIns = {
						type: NAVIGATE,
						params: {
							screen: 'Detail',
							params:{
								goodsCode: val.goodsCode
							}
						}
					}
					window.postMessage(JSON.stringify(goBackIns));
				} else if(!this.isApp) {
					this.$router.push({
						path: 'detail?code=' + val.goodscode
					})
				}

			},
			//加入购物车
			addcar(data) {
				let titleConfig = JSON.stringify({
					type: ADD_CAR,
					params: {
						page: 'Activity',
						"detailInfo": data,
					}
				});
				window.postMessage(titleConfig);
			},
		},
		computed: {
			pageName() {
				return getPageNameOrVirtualPageName();
			},
			TwoproductStyle() {
				if(this.twoproid) {
					if(parseInt(this.getCurrentVDO(this.twoproid, this.pageName).config.TwoproductStyle) == 0) {
						return 'E';
					} else if(parseInt(this.getCurrentVDO(this.twoproid, this.pageName).config.TwoproductStyle) == 1) {
						return 'H';
					} else if(parseInt(this.getCurrentVDO(this.twoproid, this.pageName).config.TwoproductStyle) == 2) {
						return 'I';
					}

				}
			},
			env() {
				return window.environment; // 获取是否是编辑状态
			},
			isApp() {
				return window.isApp;
			}
		},

		watch: {
			goodsInfo() {
				this.goodsCodearrey()

			},
		},
		mounted() {
			this.goodsCodearrey()
		},
	}
</script>

<style>
	.activitiesTwoPro .proul {
		padding: 0.75rem;
		position: relative;
	}
</style>